package dhz.storage.models;

import java.util.Calendar;
import java.util.Date;

public class UploadTask {
    // 这个上传任务的名字是什么
    private String taskName;
    // 这个上传任务总大小是多少
    private long totalSize;
    // 最后一次更新的时候，这个上传任务已经完成了多少
    private long finished;
    // 最后一次更新是什么时候
    private Calendar lastUpdate;
    // 这次更新到上次更新之间传了多少字节
    private double velocity;

    /**
     * 新建一个上传任务
     * */
    public UploadTask(){
        this.totalSize = 0;
        this.finished = 0;
        this.velocity = 0;
        lastUpdate = Calendar.getInstance();
    }

    public UploadTask(String n, long t, long f, Calendar lu, double v){
        this.taskName = n;
        this.totalSize = t;
        this.finished = f;
        this.lastUpdate = lu;
        this.velocity = v;
    }

    public void setName(String name){
        this.taskName = name;
    }

    /**
     * 更新这个上传任务
     * @param f 这次已经完成了多少字节
     * */
    public void update(long f, long l){
        this.totalSize = l;
        Calendar now = Calendar.getInstance();
        long former = lastUpdate.getTimeInMillis(), current = now.getTimeInMillis();
        if(former != current){
            velocity = 1.0 * (f - this.finished) / (now.getTimeInMillis() - lastUpdate.getTimeInMillis());
        }
        this.lastUpdate = now;
        this.finished = f;
    }

    public long getTotalSize() {
        return totalSize;
    }

    public long getFinished() {
        return finished;
    }

    public double getVelocity() {
        return velocity;
    }

    public String getTaskName(){
        return this.taskName;
    }
}
